package com.lab.controller.admin;

import com.lab.common.Result;
import com.lab.dto.LoginDTO;
import com.lab.service.LoginService;
import com.lab.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/login")
@Slf4j
@Api(tags = "统一登录接口（无需输入角色）")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping
    @ApiOperation("统一登录（仅需账号和密码，自动识别角色）")
    public Result<LoginVO> login(@Validated @RequestBody LoginDTO dto) {
        try {
            LoginVO loginVO = loginService.login(dto);
            return Result.success(loginVO, "登录成功（角色：" + loginVO.getRole() + "）");
        } catch (RuntimeException e) {
            log.error("登录失败：{}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}